package com.ss.android.article.base.feature.update.activity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.bytedance.common.utility.UIUtils;
import com.ss.android.account.b.OnAccountRefreshListener;
import com.ss.android.account.Spipe;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.ui.ImeFrameLayout;
import com.ss.android.article.browser.R;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.newmedia.a.WeakDialogListener;
import com.ss.android.newmedia.a.DialogListener;

import java.lang.ref.WeakReference;

public final class a extends Dialog implements OnAccountRefreshListener, ImeFrameLayout.OnImeEventListener {
    public interface a$a {
        void a();
    }

    public interface b {
        void a(com.ss.android.article.base.feature.update.a.b arg1);
    }

    final class c extends Handler {
        private WeakReference a;

        public c(a arg2) {

            this.a = new WeakReference(arg2);
        }

        public final void handleMessage(Message arg6) {
            int v4 = View.GONE;
            Object v0 = this.a.get();
            if (v0 == null) {
                return;
            }

            if (!((a) v0).f) {
                ((a) v0).setCancelable(true);
                ((a) v0).i.setVisibility(v4);
                return;
            }

            if (arg6.obj == null) {
                ((a) v0).setCancelable(true);
                ((a) v0).i.setVisibility(v4);
                return;
            }

            Object v1 = null;
            if ((arg6.obj instanceof com.ss.android.article.base.feature.update.a.b)) {
                v1 = arg6.obj;
            }

            switch (arg6.what) {
                case 1005: {
                    ((a) v0).setCancelable(true);
                    ((a) v0).i.setVisibility(v4);
                    if (v1 != null) {
                        ((a) v0).h.setText("");
                        ((a) v0).c();
                        if (((a) v0).isShowing()) {
                            ((a) v0).dismiss();
                        }

                        android.support.design.a.showImageToast(((a) v0).b, R.string.ss_post_ok, R.drawable.doneicon_popup_textpage);
                        try {
                            if (((a) v0).k == null) {
                                return;
                            }

                            if (v1 == null) {
                                return;
                            }

                            ((a) v0).k.a(((com.ss.android.article.base.feature.update.a.b) v1));
                        } catch (Exception v0_1) {
                        }
                    }
                    return;
                }
                case 1006: {
                    ((a) v0).setCancelable(true);
                    ((a) v0).i.setVisibility(v4);
                    if (v1 == null) {
                        return;
                    }
                    android.support.design.a.showImageToast(((a) v0).b, R.string.ss_post_fail, R.drawable.close_popup_textpage);
                    ((a) v0).c();
                    return;
                }
            }

            ((a) v0).setCancelable(true);
            ((a) v0).i.setVisibility(v4);
            return;


        }
    }

    Spipe a;
    Context b;
    Resources c;
    com.ss.android.article.base.feature.update.a.b d;
    a$a e;
    boolean f;
    InputMethodManager g;
    EditText h;
    View i;
    int j;
    b k;
    static String l = null;
    static String m = null;
    private AppData n;
    private ImeFrameLayout o;
    private TextView p;
    private View q;
    private TextView r;
    private ViewGroup s;
    private boolean t;
    private Handler u;
    private DialogListener v;

    public a(Activity arg3) {
        super(((Context) arg3));
        this.f = true;
        this.j = 0;
        this.u = new c(this);
        this.v = new com.ss.android.article.base.feature.update.activity.b(this);
        this.n = AppData.getInstance();
        this.b = ((Context) arg3);
        this.a = Spipe.inst();
        this.a.addOnAccountRefreshListener(((OnAccountRefreshListener) this));
        this.c = this.b.getResources();
        this.setOwnerActivity(arg3);
        if ((arg3 instanceof b)) {
            this.k = ((b) arg3);
        }

        WeakDialogListener v0 = new WeakDialogListener(this.v);
        this.setOnShowListener(((DialogInterface.OnShowListener) v0));
        this.setOnDismissListener(((DialogInterface.OnDismissListener) v0));
    }

    public final void a(com.ss.android.article.base.feature.update.a.b arg2) {
        this.d = arg2;
        this.j = 1;
        this.show();
    }

    static void a(String arg0) {
        l = arg0;
    }

    final void a() {
        if (this.o != null) {
            AppData.getInstance();
            boolean v0 = AppData.isNightMode();
            if (this.t != v0) {
                this.t = v0;
                Resources v0_1 = this.b.getResources();
                this.o.setBackgroundColor(v0_1.getColor(com.ss.android.article.browser.R.color.wl));
                this.h.setHintTextColor(v0_1.getColor(com.ss.android.article.browser.R.color.wo));
                this.h.setTextColor(v0_1.getColor(com.ss.android.article.browser.R.color.wu));
                UIUtils.setBackgroundResource(this.s, R.drawable.dk);
                UIUtils.setMaxLines(this.p, R.drawable.cc);
                UIUtils.setBackgroundResource(this.q, R.color.ci);
                this.p.setTextColor(v0_1.getColorStateList(com.ss.android.article.browser.R.color.ssxinzi7));
                this.r.setTextColor(v0_1.getColor(com.ss.android.article.browser.R.color.ssxinzi3));
            }
        }
    }

    static void b(String arg0) {
        m = arg0;
    }

    final void b() {
        int v0 = 400 - this.h.getText().length();
        if (v0 < 0) {
            this.r.setVisibility(View.VISIBLE);
            this.r.setText(String.valueOf(v0));
        } else {
            this.r.setVisibility(View.GONE);
        }

        this.c();
    }

    final void c() {
        TextView v1 = this.p;
        boolean v0 = this.h.getText().toString().trim().length() > 0 ? true : false;
        v1.setEnabled(v0);
    }

    final void d() {
        String v5 = null;
        int v3 = R.drawable.close_popup_textpage;
        if (!NetworkUtils.isNetworkAvailable(this.b)) {
            UIUtils.showImageToast(this.b, v3, R.string.ss_comment_error_no_network);
        } else if (this.d == null) {
            this.dismiss();
        } else {
            String v0 = this.h.getText().toString();
            if (v0 != null) {
                v0 = v0.trim();
            }

            if (v0 != null && v0.length() != 0) {
                if (v0.length() > 400) {
                    UIUtils.showImageToast(this.b, v3, R.string.detail_comment_too_long);
                } else {
                    this.i.setVisibility(View.VISIBLE);
                    this.setCancelable(false);
                    this.d.c = v0;
                    MobClickCombiner.onEvent(this.b, "xiangping", "update_write_confirm");
                    new com.ss.android.article.base.feature.update.b.b(this.b, this.u, this.d).start();
                    if (this.n != null) {
                        switch (this.j) {
                            case 1: {
                                l = v5;
                                break;
                            }
                            case 2: {
                                m = v5;
                                break;
                            }
                        }
                    }
                    this.a.b(((OnAccountRefreshListener) this));
                }

                return;
            }

            this.h.setText("");
            UIUtils.showImageToast(this.b, v3, R.string.ss_error_empty_content);
        }
    }

    public final void i() {
        if (this.isShowing()) {
            this.dismiss();
        }
    }

    public final void onAccountRefresh(boolean arg1, int arg2) {
        if (arg1) {
            this.d();
        }
    }

    protected final void onCreate(Bundle arg6) {
        super.onCreate(arg6);
        this.requestWindowFeature(1);
        this.setContentView(com.ss.android.article.browser.R.layout.j1);
        this.getWindow().setBackgroundDrawableResource(com.ss.android.article.browser.R.drawable.e);
        this.getWindow().setLayout(-1, -2);
        this.getWindow().setGravity(80);
        this.getWindow().setSoftInputMode(5);
        this.g = (InputMethodManager) this.getOwnerActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        this.setCancelable(true);
        this.setCanceledOnTouchOutside(true);
        this.o = this.findViewById(com.ss.android.article.browser.R.id.lc);
        this.o.setOnImeEventListener(((ImeFrameLayout.OnImeEventListener) this));
        this.s = this.findViewById(com.ss.android.article.browser.R.id.ac6);
        this.r = this.findViewById(com.ss.android.article.browser.R.id.ac7);
        this.h = this.findViewById(com.ss.android.article.browser.R.id.content);
        this.h.addTextChangedListener(new com.ss.android.article.base.feature.update.activity.c(this));
        if (this.d != null) {
            if (!TextUtils.isEmpty(this.d.f)) {
                this.h.setHint(String.format(this.c.getString(com.ss.android.article.browser.R.string.lb), this.d.f));
            } else {
                this.h.setHint("");
            }
        }

        this.i = this.findViewById(com.ss.android.article.browser.R.id.progressbar);
        this.q = this.findViewById(com.ss.android.article.browser.R.id.top_divider);
        this.p = this.findViewById(com.ss.android.article.browser.R.id.li);
        this.p.setOnClickListener(new d(this));
        this.b();
        this.a();
    }

    public final void onStop() {
        if (((Activity) this.b).isFinishing()) {
            this.f = false;
        }
    }
}

